<?php

namespace app\api\controller\v1\plan;

use app\model\plan\course\StudyCourse;
use app\Request;
use app\services\plan\PlanService;

class CourseController
{
    public function study(Request $request)
    {
        list($studyId) = $request->getMore([
            ['study_id', 0] ,

        ], true);

        $where[] = ['id' , '=' , $studyId] ;
        $where[] = ['staff_id' , '=' , $request->staffId()] ;
        $info = StudyCourse::where($where)->find() ;
        if($info['study_status'] == 9){
            return app('json')->success();
        }
        $info->study_status  =  1 ;
        $info->save() ;

        return app('json')->success();
    }
    public function progress(Request $request)
    {
        list($studyId, $time) = $request->getMore([
            ['study_id', 0] ,
            ['time', 0] ,
        ], true);
        if($time <=0){
            return app('json')->success();
        }
        $where[] = ['id' , '=' , $studyId] ;
        $where[] = ['staff_id' , '=' , $request->staffId()] ;
        $info = StudyCourse::where($where)->find() ;
        $video_progress_max_time = $time ;
        if($info['video_progress_max_time'] > $time)
        {
            $video_progress_max_time = $info['video_progress_max_time'] ;
        }

        $info->video_progress_time = $time ;
        $info->video_progress_max_time = $video_progress_max_time ;

        $info->save() ;
        return app('json')->success();
    }
    public function end(Request $request)
    {
        list($studyId , $time) = $request->getMore([
            ['study_id', 0] ,
            ['time', 0] ,
        ], true);
        $video_progress_max_time = $time ;

        $where[] = ['id' , '=' , $studyId] ;
        $where[] = ['staff_id' , '=' , $request->staffId()] ;
        $info = StudyCourse::where($where)->find() ;

        if($info['video_progress_max_time'] > $time)
        {
            $video_progress_max_time = $info['video_progress_max_time'] ;
        }
        $info->end_time = time() ;
        $info->study_status = 9 ;
        $info->save();

        $planService = new PlanService() ;
        $planService->progress($info['plan_id'] ,$request->staffId()) ;


        return app('json')->success();
    }





}